package swing;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

import javax.swing.JFrame;

public class MyFrame extends JFrame {

	LinkedList<Object> events = new LinkedList<Object>();
	private int i = 0;

	public MyFrame() {
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 400);
		addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				events.add(e);
			}

			@Override
			public void keyReleased(KeyEvent e) {
				events.add(e);
			}

			@Override
			public void keyPressed(KeyEvent e) {
				events.add(e);
			}
		});
	}

	public LinkedList<Object> getEvents() {
		LinkedList<Object> out = events;
		i++;
		events = new LinkedList<Object>();
		return out;
	}

	public int getI() {
		return i;
	}

	static int count;

	public static void main(String[] args) throws InterruptedException {
		final MyFrame f = new MyFrame();
		Thread[] th = new Thread[1];
		for (count = 0; count < th.length; count++)
			th[count] = new Thread() {
				int i = count;

				@Override
				public void run() {
					while (true) {
						System.out.println("Thread" + i);
						System.out.println(f.getI());
						for (Object o : f.getEvents())
							System.out.println(o);
						System.out.println();
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {}
					}
				}
			};
		th[0].start(); 
	}
}